#!/bin/bash

# Name all the files that you need to check.
SRC="\
fixpoint.c \
floatingpoint.c \
Makefile.fixpoint \
Makefile.floatingpoint "

echo "** Quadpod tools **"

if [ -n "$1" ]
then
	echo -n "Detecting configuration ... "
else 
	echo "failed"
	echo "ERROR: no configuration selected. Use 'floating', 'fixpoint', 'check'. Bailing out."
	exit -1
fi

if  echo "$1" | grep -q "check" 
then
	configuration="check"
fi

if  echo "$1" | grep -q "fixpoint" 
then
	cp -f Makefile.fixpoint Makefile
	configuration="fixpoint"
fi

if  echo "$1" | grep -q "floating" 
then
	cp -f Makefile.floatingpoint Makefile
	configuration="math"
fi

if [ -n "$configuration" ]
then
	echo " OK"
	echo "Looks good, using configuration: $configuration"
else
	echo " failed"
	echo "ERROR: unknown configuration selected. Supported is 'check', 'fixpoint', 'floating'. Bailing out."
	exit -1
fi

#
# List all the files here ...
#
for file in $SRC
do
	echo -n "checking $file ... "
	if [ -f $file ]
	then
		echo " OK"
	else
		echo " failed"
		echo "Perform svn checkout command first"
		exit -1
	fi
done

if echo "$configuration" | grep -q "check" 
then
	echo "** STOP here: all OK **"
	exit -1
fi

if echo "$configuration" | grep -q "fixpoint"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.fixpoint Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi

if echo "$configuration" | grep -q "floating"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.floatingpoint Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi


echo "Makefile for configuration $configuration has been created. Use command"
echo " "
echo "   make [debug|release]"
echo " "
echo "** configuration finished OK **"
